package com.skydroid.testflightcontroller;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import com.MAVLink.MAVLinkPacket;
import com.MAVLink.Messages.MAVLinkMessage;
import com.MAVLink.Parser;
import com.MAVLink.common.msg_param_request_read;
import com.MAVLink.common.msg_param_value;
import com.skydroid.fpvplayer.MyLogger;
import com.skydroid.rcsdk.PipelineManager;
import com.skydroid.rcsdk.RCSDKManager;
import com.skydroid.rcsdk.SDKManagerCallBack;
import com.skydroid.rcsdk.comm.CommListener;
import com.skydroid.rcsdk.common.DeviceType;
import com.skydroid.rcsdk.common.Uart;
import com.skydroid.rcsdk.common.error.SkyException;
import com.skydroid.rcsdk.common.pipeline.Pipeline;

import java.util.Date;

/**
 * @author 咔一下
 * @date 2023/11/15 11:16
 * @email 1501020210@qq.com
 * @describe
 */
public class RcsdkTestActivity extends AppCompatActivity {

    private Pipeline serialPortConnection;
    private final Parser parser = new Parser();
    private final MutableLiveData<String> infoLiveData = new MutableLiveData<>();
    private final String TEST_PARAM = "SYSID_ENFORCE";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        serialPortConnection = PipelineManager.INSTANCE.createPipeline(DeviceType.H12,Uart.UART0);
        serialPortConnection.setOnCommListener(new CommListener() {
            @Override
            public void onConnectSuccess() {
                MyLogger.kLog().d("串口连接成功");
            }

            @Override
            public void onConnectFail(SkyException e) {
                MyLogger.kLog().e(e);
            }

            @Override
            public void onDisconnect() {
                MyLogger.kLog().d("串口断开连接");
            }

            @Override
            public void onReadData(byte[] bytes) {
                if (bytes == null){
                    return;
                }
                for (byte b : bytes) {
                    MAVLinkPacket packet = parser.mavlink_parse_char(b & 0xFF);
                    if (packet == null){
                        continue;
                    }
                    MAVLinkMessage msg = packet.unpack();
                    //打印消息
                    MyLogger.kLog().d(msg);
                    onMessageHandle(msg);
                }
            }
        });
        //内部已实现自动断开重连逻辑，开发者无需关注断开重连功能
        PipelineManager.INSTANCE.connectPipeline(serialPortConnection);

        findViewById(R.id.tv_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                msg_param_request_read msg = new msg_param_request_read();
                msg.param_index = -1;
                msg.setParam_Id(TEST_PARAM);
                msg.isMavlink2 = true;
                serialPortConnection.writeData(msg.pack().encodePacket());
            }
        });

        TextView tvInfo = findViewById(R.id.tv_info);
        infoLiveData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                tvInfo.setText(s);
            }
        });
    }

    private void onMessageHandle(MAVLinkMessage message){
        if (message.msgid == msg_param_value.MAVLINK_MSG_ID_PARAM_VALUE){
            msg_param_value param_value = (msg_param_value)message;
            String paramId = param_value.getParam_Id();
            if (TEST_PARAM.equals(paramId)){
                infoLiveData.postValue(paramId + " = "  + param_value.param_value + " - " + new Date());
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PipelineManager.INSTANCE.disconnectPipeline(serialPortConnection);
    }
}
